/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jpExportarAsistencias.java
 *
 * Created on 24/10/2011, 01:16:25 AM
 */

package Control;

import administracion.delegate.GestionPedidosDelegate;
import administracion.helper.ExcelTableExporter;
import administracion.helper.Utiles_MensajesDialogo;
import java.io.File;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JFormattedTextField;
import javax.swing.JInternalFrame;
import javax.swing.text.MaskFormatter;
import suministros.ejb30.TO.AsistenciaTO;

/**
 *
 * @author Renato
 */
public class jpExportarAsistencias extends javax.swing.JPanel {

    private JInternalFrame jifpadre;
     private MaskFormatter mascaraFechaIngreso;
    /** Creates new form jpExportarAsistencias */
    public jpExportarAsistencias() {
        construirMascaras();
        initComponents();
    }

    public void setPadre(JInternalFrame padre){
        this.jifpadre=padre;
    }
     private void construirMascaras() {
        try {
            this.mascaraFechaIngreso = new MaskFormatter("##/##/####");
        } catch (Exception ex) {
        Logger.getLogger(jpExportarAsistencias.class.getName()).log(Level.SEVERE, null, ex);
        ex.printStackTrace();
        }
  }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jftfFechaInicio = new JFormattedTextField(this.mascaraFechaIngreso);
        jLabel2 = new javax.swing.JLabel();
        jftfFechaFin = new JFormattedTextField(this.mascaraFechaIngreso);
        jbtnCargarTrabajadores = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtAsistencia = new javax.swing.JTable();
        jbtnProcesar = new javax.swing.JButton();
        jbtnSalir = new javax.swing.JButton();

        jLabel1.setText("FECHA INICIO");

        jLabel2.setText("FECHA FIN");

        jbtnCargarTrabajadores.setFont(new java.awt.Font("Arial", 1, 14));
        jbtnCargarTrabajadores.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/fife/plaf/Office2003/options.gif"))); // NOI18N
        jbtnCargarTrabajadores.setText("Cargar");
        jbtnCargarTrabajadores.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCargarTrabajadoresActionPerformed(evt);
            }
        });
        jbtnCargarTrabajadores.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbtnCargarTrabajadoresKeyPressed(evt);
            }
        });

        jtAsistencia.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "DNI", "NOMBRES", "AREA", "FECHA", "H.ENTRADA", "H. SALIDA", "H.ORDINARIAS", "H. NOCTURNAS", "H. EXTRA 25%", "H. EXTRA 35%", "DOMINICAL", "FERIADO"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jtAsistencia);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 729, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 729, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 312, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        jbtnProcesar.setFont(new java.awt.Font("Tahoma", 1, 11));
        jbtnProcesar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/fife/plaf/Office2003/replace.gif"))); // NOI18N
        jbtnProcesar.setText("Exportar");
        jbtnProcesar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnProcesarActionPerformed(evt);
            }
        });

        jbtnSalir.setFont(new java.awt.Font("Tahoma", 1, 11));
        jbtnSalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/fife/plaf/Office2003/closeall.gif"))); // NOI18N
        jbtnSalir.setText("Salir");
        jbtnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnSalirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 729, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jftfFechaInicio, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jftfFechaFin, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbtnCargarTrabajadores, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(261, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(545, 545, 545)
                .addComponent(jbtnProcesar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnSalir))
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 374, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jftfFechaInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jftfFechaFin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnCargarTrabajadores))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnSalir)
                    .addComponent(jbtnProcesar)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnCargarTrabajadoresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnCargarTrabajadoresActionPerformed
        this.limpiarTabla();
        try{
            List<AsistenciaTO> resultado=GestionPedidosDelegate.getInstance().
                    getAsistenciaEntreFechas(this.jftfFechaInicio.getValue().toString().trim(),
                    this.jftfFechaFin.getValue().toString().trim());
            for(AsistenciaTO datos:resultado){
                ((javax.swing.table.DefaultTableModel)jtAsistencia.getModel()).addRow(
                        new Object[]{datos.getDni(),datos.getNombre(),datos.getArea(),datos.getFecha(),
                        datos.getHentrada(),datos.getHsalida(),datos.getHordinaria(),datos.getHnocturna(),
                        datos.getHextra25(),datos.getHextra35(),datos.getDomingo(),datos.getFeriado()});
            }
        }catch(Exception e){
            Utiles_MensajesDialogo.mensajeError("No se pudo importar el archivo\n"+e.getMessage());
            e.printStackTrace();
        }

    }//GEN-LAST:event_jbtnCargarTrabajadoresActionPerformed

    private void limpiarTabla(){
        jtAsistencia.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "DNI", "NOMBRES", "AREA", "FECHA", "H.ENTRADA", "H. SALIDA", "H.ORDINARIAS", "H. NOCTURNAS", "H. EXTRA 25%", "H. EXTRA 35%", "DOMINICAL", "FERIADO"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
    }
    private void jbtnCargarTrabajadoresKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbtnCargarTrabajadoresKeyPressed
        // TODO add your handling code here:
}//GEN-LAST:event_jbtnCargarTrabajadoresKeyPressed

    private void jbtnProcesarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnProcesarActionPerformed
        JFileChooser jfc = new JFileChooser();
        jfc.setDialogType(1);
        int i = jfc.showDialog(null, "Guardar Excel(XLS)");
        if (i == 1) return;
        ExcelTableExporter sd =   new ExcelTableExporter(this.jtAsistencia, new File(jfc.getSelectedFile().getPath() + ".xls"),
                "ASISTENCIA","ASISTENCIA DEL "+this.jftfFechaInicio.getValue().toString()+" al "+this.jftfFechaFin.getValue().toString());
        sd.export();
    }//GEN-LAST:event_jbtnProcesarActionPerformed

    private void jbtnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnSalirActionPerformed
        this.jifpadre.dispose();

}//GEN-LAST:event_jbtnSalirActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtnCargarTrabajadores;
    private javax.swing.JButton jbtnProcesar;
    private javax.swing.JButton jbtnSalir;
    private javax.swing.JFormattedTextField jftfFechaFin;
    private javax.swing.JFormattedTextField jftfFechaInicio;
    private javax.swing.JTable jtAsistencia;
    // End of variables declaration//GEN-END:variables

}
